﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;

namespace FinalProject
{
    public partial class WebForm10 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                try
                {
                    InitForm();
                    string str = Request["exist"];
                    if (str == "true")
                    {
                        InitHoSo();
                    }
                    else
                    {
                        Utility.indexHoSo = -1;
                    }
                }
                catch { }                
            }
            else
            {
                btnTTC_TiepTuc.Click += new EventHandler(btnTTC_TiepTuc_Click);
                cbxSoNamKinhNghiem.CheckedChanged += new EventHandler(cbxSoNamKinhNghiem_CheckedChanged);                
            }

            RadioButton1.CheckedChanged += new EventHandler(RadioButton1_CheckedChanged);
            RadioButton2.CheckedChanged += new EventHandler(RadioButton2_CheckedChanged);
            
        }

        void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton1.Checked = RadioButton2.Checked ? false : true;
            tbxMucLuong.Enabled = RadioButton1.Checked;
            if (!RadioButton1.Checked)
                tbxMucLuong.Text = "";
        }

        void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton2.Checked = RadioButton1.Checked ? false : true;
            tbxMucLuong.Enabled = RadioButton1.Checked;
            if (!RadioButton1.Checked)
                tbxMucLuong.Text = "";
        }

        private void GetLstNgoaiNgu(ref List<NgoaiNgu> lstNgoaiNgu, ref List<TrinhDoNgoaiNgu> lstTrinhDoNgoaiNgu, 
            DropDownList ddlNgoaiNgu, DropDownList ddlTrinhDoNgoaiNgu, ref Image img, ref Label lblAlert)
        {
            if (CheckDropDownList(ddlNgoaiNgu))
            {
                lstNgoaiNgu.Add(new NgoaiNgu(Utility.LstNgoaiNgu[ddlNgoaiNgu.SelectedIndex - 1].ID,
                    Utility.LstNgoaiNgu[ddlNgoaiNgu.SelectedIndex - 1].Ten));

                if (CheckDropDownList(ddlTrinhDoNgoaiNgu))
                {
                    lstTrinhDoNgoaiNgu.Add(new TrinhDoNgoaiNgu(Utility.LstTrinhDoNgoaiNgu[ddlTrinhDoNgoaiNgu.SelectedIndex - 1].ID,
                        Utility.LstTrinhDoNgoaiNgu[ddlTrinhDoNgoaiNgu.SelectedIndex - 1].Ten));

                    img.Visible = false;
                    lblAlert.Visible = false;
                }
                else
                {
                    img.Visible = true;
                    lblAlert.Visible = true;
                }
            }
        }

        void btnTTC_TiepTuc_Click(object sender, EventArgs e)
        {
            Image1.Visible = !CheckSoNamKinhNghiem(tbxSoNamKinhNghiem.Text);           
            Image2.Visible = !CheckViTriMongMuon(tbxViTriMongMuon.Text);                        
            Image3.Visible = !CheckDropDownList(DdlCapBacMongMuon);
            Image4.Visible = !CheckDropDownList(ddlNoiLamViec);
            Image5.Visible = !CheckDropDownList(ddlNganhNghe);

            lblAlertSoNamKinhNghiem.Visible = !CheckSoNamKinhNghiem(tbxSoNamKinhNghiem.Text);
            lblAlertViTriMongMuon.Visible = !CheckViTriMongMuon(tbxViTriMongMuon.Text);            
            lblAlertCapBacMongMuon.Visible = !CheckDropDownList(DdlCapBacMongMuon);
            lblAlertNoiLamViec.Visible = !CheckDropDownList(ddlNoiLamViec);
            lbtAlertNganhNghe.Visible = !CheckDropDownList(ddlNganhNghe);


            #region Ngoại ngữ và Trình độ ngoại ngữ có thể null
            List<NgoaiNgu> lstNgoaiNgu = new List<NgoaiNgu>();
            List<TrinhDoNgoaiNgu> lstTrinhDoNN = new List<TrinhDoNgoaiNgu>();

            GetLstNgoaiNgu(ref lstNgoaiNgu, ref lstTrinhDoNN, DdlNgoaiNgu1, DdlTrinhDoNN1, ref Image6, ref lblAlertTrinhDoNN1);
            GetLstNgoaiNgu(ref lstNgoaiNgu, ref lstTrinhDoNN, DdlNgoaiNgu2, DdlTrinhDoNN2, ref Image7, ref lblAlertTrinhDoNN2);
            GetLstNgoaiNgu(ref lstNgoaiNgu, ref lstTrinhDoNN, DdlNgoaiNgu3, DdlTrinhDoNN3, ref Image8, ref lblAlertTrinhDoNN3);
            #endregion

            if (!Image1.Visible && !Image2.Visible && !Image1.Visible && !Image2.Visible && !Image5.Visible
                && !Image6.Visible && !Image7.Visible && !Image8.Visible)
            {
                int iSoNamKinhNghiem = int.Parse(tbxSoNamKinhNghiem.Text);
                string strViTriMongMuon = tbxViTriMongMuon.Text;

                #region Chức vụ - cấp bậc mong muốn: class ChucVu
                ChucVu cv = new ChucVu(Utility.LstChucVu[DdlCapBacMongMuon.SelectedIndex - 1].ID,
                    Utility.LstChucVu[DdlCapBacMongMuon.SelectedIndex - 1].Ten);
                #endregion

                #region Nơi làm việc: List<TinhThanh>
                List<TinhThanh> lstNoiLamViec = new List<TinhThanh>();
                //lstNoiLamViec.Add(new TinhThanh(Utility.LstTinhThanh[ddlNoiLamViec.SelectedIndex - 1].ID,
                //Utility.LstTinhThanh[ddlNoiLamViec.SelectedIndex - 1].Ten));
                if (lblNoilamviec1.Visible)
                    lstNoiLamViec.Add(Utility.getTinhThanhTTByTen(lblNoilamviec1.Text));
                if (lblNoilamviec2.Visible)
                    lstNoiLamViec.Add(Utility.getTinhThanhTTByTen(lblNoilamviec2.Text));
                if (lblNoilamviec3.Visible)
                    lstNoiLamViec.Add(Utility.getTinhThanhTTByTen(lblNoilamviec3.Text));
                #endregion

                #region Ngành nghề: List<NganhNghe>
                List<NganhNghe> lstNganhNghe = new List<NganhNghe>();
                //lstNganhNghe.Add(new NganhNghe(Utility.LstNganhNghe[ddlNganhNghe.SelectedIndex - 1].ID,
                //Utility.LstNganhNghe[ddlNganhNghe.SelectedIndex - 1].Ten));
                if (lblNganhNghe1.Visible)
                    lstNganhNghe.Add(Utility.getNganhNgheNNByTen(lblNganhNghe1.Text));
                if (lblNganhNghe2.Visible)
                    lstNganhNghe.Add(Utility.getNganhNgheNNByTen(lblNganhNghe2.Text));
                if (lblNganhNghe3.Visible)
                    lstNganhNghe.Add(Utility.getNganhNgheNNByTen(lblNganhNghe3.Text));
                #endregion

                #region Cho phép tìm kiếm: boolean
                bool isChoPhepTimKiem = ckbChoPhepTimKiem.Checked;
                #endregion

                #region Mức lương hay thương lượng
                int iMucLuong = 0;
                if (RadioButton1.Checked)
                {
                    iMucLuong = int.Parse(RadioButton1.Text);
                }
                #endregion

                if (Utility.indexHoSo == -1)
                {
                    Utility.HoSoCurrent = new HoSo(iSoNamKinhNghiem, lstNgoaiNgu, lstTrinhDoNN, strViTriMongMuon,
                        cv, lstNoiLamViec, lstNganhNghe, (float)iMucLuong, isChoPhepTimKiem);
                    Utility.HoSoCurrent.ThongTinLienHe = new ThongTinLienHe(Utility.UserCurrent.StrLastname, Utility.UserCurrent.StrFirstname, Utility.UserCurrent.DtimeBirthday, Utility.UserCurrent.ISex == 1 ? true : false, "Image/sam_resumephoto.png", new RootObject(), "", new RootObject(), new RootObject(), new RootObject(), "", Utility.UserCurrent.StrEmail);
                    Response.Redirect("TaoHoSoThongTinCaNhan.aspx?exist=false");
                }
                else
                {
                    Utility.HoSoCurrent.UpdateThongTinChung(iSoNamKinhNghiem, lstNgoaiNgu, lstTrinhDoNN, strViTriMongMuon,
                        cv, lstNoiLamViec, lstNganhNghe, (float)iMucLuong, isChoPhepTimKiem);         
           
                    Response.Redirect("TaoHoSoThongTinCaNhan.aspx?exist=true");
                }
                errorDiv.Style["display"] = "none";
            }
            else
            {
                errorDiv.Style["display"] = "block";
            }
            
        }
             
        public bool CheckSoNamKinhNghiem(string strSoNam)
        {
            double Num;
            return double.TryParse(strSoNam,out Num);            
        }    
              
        public bool CheckViTriMongMuon(string strViTri)
        {
            return strViTri == "" ? false : true;
        }

        public bool CheckDropDownList(DropDownList dropdownlist)
        {
            return dropdownlist.SelectedIndex < 1 ? false : true;
        }

        /*
        public bool CheckCapBacMongMuon(DropDownList dropdownlist)
        {
            return dropdownlist.SelectedIndex <1 ? false : true;
        }

        public bool CheckNoiLamViec(DropDownList dropdownlist)
        {
            return dropdownlist.SelectedIndex < 1 ? false : true;
        }

        public bool CheckNganhNghe(DropDownList dropdownlist)
        {
            return dropdownlist.SelectedIndex < 1 ? false : true;
        }
        */

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            if (DdlNgoaiNgu2.Visible == false)
            {
                DdlNgoaiNgu2.Visible = true;
                DdlTrinhDoNN2.Visible = true;                
                ibtnTrinhDoNN2.Visible = true;
            }
            else
            {
                if (DdlNgoaiNgu2.Visible == true && DdlNgoaiNgu3.Visible == false)
                {
                    DdlNgoaiNgu3.Visible = true;
                    DdlTrinhDoNN3.Visible = true;
                    ibtnTrinhDoNN3.Visible = true;

                }
            }

            if (DdlNgoaiNgu2.Visible == true && DdlNgoaiNgu3.Visible == true)
            {
                lbtnThemTrinhDoNN.Visible = false;
                ibtnThemTrinhDoNN.Visible = false;

            }
        }

        protected void ibtnTrinhDoNN1_Click(object sender, ImageClickEventArgs e)
        {
            //DdlTrinhDoNN1.Visible = false;
            //DdlNgoaiNgu1.Visible = false;
            //ibtnTrinhDoNN1.Visible = false;
        }

        protected void ibtnTrinhDoNN2_Click(object sender, ImageClickEventArgs e)
        {
            DdlTrinhDoNN2.Visible = false;
            DdlNgoaiNgu2.Visible = false;
            ibtnTrinhDoNN2.Visible = false;
            lbtnThemTrinhDoNN.Visible = true;
            ibtnThemTrinhDoNN.Visible = true;
            Image7.Visible = false;
            lblAlertTrinhDoNN2.Visible = false;
        }

        protected void ibtnTrinhDoNN3_Click(object sender, ImageClickEventArgs e)
        {
            DdlTrinhDoNN3.Visible = false;
            DdlNgoaiNgu3.Visible = false;
            ibtnTrinhDoNN3.Visible = false;
            lbtnThemTrinhDoNN.Visible = true;
            ibtnThemTrinhDoNN.Visible = true;
            Image8.Visible = false;
            lblAlertTrinhDoNN3.Visible = false;
        }

        public void InitForm()
        {
            RadioButton2.Checked = true;
            RadioButton1.Checked = false;
            RadioButton1.AutoPostBack = true;
            RadioButton2.AutoPostBack = true;
            ckbChoPhepTimKiem.AutoPostBack = true;
            cbxSoNamKinhNghiem.AutoPostBack = true;           
           

            #region Tinh Thanh - Noi lam viec
            ddlNoiLamViec.AutoPostBack = true;
            Utility.LoadDropDownList(ref ddlNoiLamViec, Utility.LstTinhThanh);
            #endregion

            #region Nganh Nghe
            ddlNganhNghe.AutoPostBack = true;
            Utility.LoadDropDownList(ref ddlNganhNghe, Utility.LstNganhNghe);
            #endregion    
        
            #region Chuc vu - Cap bac
            DdlCapBacMongMuon.AutoPostBack = true;
            Utility.LoadDropDownList(ref DdlCapBacMongMuon, Utility.LstChucVu);
            #endregion   

            #region Ngoai ngu
            DdlNgoaiNgu1.AutoPostBack = true;
            Utility.LoadDropDownList(ref DdlNgoaiNgu1, Utility.LstNgoaiNgu);
            #endregion  

            #region Trinh Do Ngoai Ngu
            DdlTrinhDoNN1.AutoPostBack = true;
            Utility.LoadDropDownList(ref DdlTrinhDoNN1, Utility.LstTrinhDoNgoaiNgu);
            #endregion  

            #region Ngoai ngu
            DdlNgoaiNgu2.AutoPostBack = true;
            Utility.LoadDropDownList(ref DdlNgoaiNgu2, Utility.LstNgoaiNgu);
            #endregion

            #region Trinh Do Ngoai Ngu
            DdlTrinhDoNN2.AutoPostBack = true;
            Utility.LoadDropDownList(ref DdlTrinhDoNN2, Utility.LstTrinhDoNgoaiNgu);
            #endregion  

            #region Ngoai ngu
            DdlNgoaiNgu3.AutoPostBack = true;
            Utility.LoadDropDownList(ref DdlNgoaiNgu3, Utility.LstNgoaiNgu);
            #endregion

            #region Trinh Do Ngoai Ngu
            DdlTrinhDoNN3.AutoPostBack = true;
            Utility.LoadDropDownList(ref DdlTrinhDoNN3, Utility.LstTrinhDoNgoaiNgu);
            #endregion  
        }

        private void InitHoSo()
        {
            #region So nam kinh nghiem
            if (Utility.HoSoCurrent.NamKinhNghiem == 0)
            {
                cbxSoNamKinhNghiem.Checked = true;
                tbxSoNamKinhNghiem.Text = "0";
                tbxSoNamKinhNghiem.Enabled = false;
            }
            else
            {
                cbxSoNamKinhNghiem.Checked = false;
                tbxSoNamKinhNghiem.Enabled = true;
                tbxSoNamKinhNghiem.Text = Utility.HoSoCurrent.NamKinhNghiem.ToString();
            }
            #endregion

            tbxViTriMongMuon.Text = Utility.HoSoCurrent.ViTriMongMuon;

            #region Cap bac mong muon
            DdlCapBacMongMuon.SelectedIndex = 0;
            if (Utility.HoSoCurrent.ChucVu != null && Utility.HoSoCurrent.ChucVu.Ten != "")
            {
                for (int i = 0; i < DdlCapBacMongMuon.Items.Count; i++)
                {
                    if (DdlCapBacMongMuon.Items[i].Text == Utility.HoSoCurrent.ChucVu.Ten)
                    {
                        DdlCapBacMongMuon.SelectedIndex = i;
                        break;
                    }
                }
            }            
            #endregion

            ckbChoPhepTimKiem.Checked = Utility.HoSoCurrent.IsChoPhepTimKiem;
        }

        protected void cbxSoNamKinhNghiem_CheckedChanged(object sender, EventArgs e)
        {
            if (cbxSoNamKinhNghiem.Checked)
            {
                tbxSoNamKinhNghiem.Text = "0";
                tbxSoNamKinhNghiem.Enabled = false;
            }
            else
            {
                tbxSoNamKinhNghiem.Text = "";
                tbxSoNamKinhNghiem.Enabled = true;
            }
        }

        protected void ddlNoiLamViec_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblNoilamviec.Visible = true;
            if (!lblNoilamviec1.Visible)
            {
                lblNoilamviec1.Text = Utility.LstTinhThanh[ddlNoiLamViec.SelectedIndex - 1].Ten;
                lblNoilamviec1.Visible = true;
                ibtnNoilamviec1.Visible = true;
            }
            else
            {
                if (!lblNoilamviec2.Visible)
                {
                    lblNoilamviec2.Text = Utility.LstTinhThanh[ddlNoiLamViec.SelectedIndex - 1].Ten;
                    lblNoilamviec2.Visible = true;
                    ibtnNoilamviec2.Visible = true;
                }
                else
                {
                    if (!lblNoilamviec3.Visible)
                    {
                        lblNoilamviec3.Text = Utility.LstTinhThanh[ddlNoiLamViec.SelectedIndex - 1].Ten;
                        lblNoilamviec3.Visible = true;
                        ibtnNoilamviec3.Visible = true;
                        ddlNoiLamViec.Enabled = false;
                    }                
                }
            }
        }

        protected void ibtnNoilamviec3_Click(object sender, ImageClickEventArgs e)
        {
            ibtnNoilamviec3.Visible = false;
            lblNoilamviec3.Visible = false;
            ddlNoiLamViec.Enabled = true;
            if (!lblNoilamviec1.Visible && !lblNoilamviec2.Visible)
            {
                lblNoilamviec.Visible = false;
            }
        }

        protected void ibtnNoilamviec2_Click(object sender, ImageClickEventArgs e)
        {
            ibtnNoilamviec2.Visible = false;
            lblNoilamviec2.Visible = false;
            ddlNoiLamViec.Enabled = true;
            if (!lblNoilamviec3.Visible && !lblNoilamviec1.Visible)
            {
                lblNoilamviec.Visible = false;
            }
        }

        protected void ibtnNoilamviec1_Click(object sender, ImageClickEventArgs e)
        {
            ibtnNoilamviec1.Visible = false;
            lblNoilamviec1.Visible = false;
            ddlNoiLamViec.Enabled = true;
            if (!lblNoilamviec3.Visible && !lblNoilamviec2.Visible)
            {
                lblNoilamviec.Visible = false;
            }
        }

        protected void ddlNganhNghe_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblNganhNghe.Visible = true;
            if (!lblNganhNghe1.Visible)
            {
                lblNganhNghe1.Text = Utility.LstNganhNghe[ddlNganhNghe.SelectedIndex - 1].Ten;
                lblNganhNghe1.Visible = true;
                ibtnNganhNghe1.Visible = true;
            }
            else
            {
                if (!lblNganhNghe2.Visible)
                {
                    lblNganhNghe2.Text = Utility.LstNganhNghe[ddlNganhNghe.SelectedIndex - 1].Ten;
                    lblNganhNghe2.Visible = true;
                    ibtnNganhNghe2.Visible = true;
                }
                else
                {
                    if (!lblNganhNghe3.Visible)
                    {
                        lblNganhNghe3.Text = Utility.LstNganhNghe[ddlNganhNghe.SelectedIndex - 1].Ten;
                        lblNganhNghe3.Visible = true;
                        ibtnNganhNghe3.Visible = true;
                        ddlNganhNghe.Enabled = false;
                    }
                }
            }
        }

        protected void ibtnNganhNghe3_Click(object sender, ImageClickEventArgs e)
        {
            ibtnNganhNghe3.Visible = false;
            lblNganhNghe3.Visible = false;
            ddlNganhNghe.Enabled = true;
            if (!lblNganhNghe1.Visible && !lblNganhNghe2.Visible)
            {
                lblNganhNghe.Visible = false;
            }
        }

        protected void ibtnNganhNghe2_Click(object sender, ImageClickEventArgs e)
        {
            if (lblNganhNghe3.Visible)
            {
                lblNganhNghe2.Text = lblNganhNghe3.Text;
                ibtnNganhNghe3_Click(sender, e);
            }
            else
            {
                ibtnNganhNghe2.Visible = false;
                lblNganhNghe2.Visible = false;
                ddlNganhNghe.Enabled = true;
                if (!lblNganhNghe3.Visible && !lblNganhNghe1.Visible)
                {
                    lblNganhNghe.Visible = false;
                }
            }
        }

        protected void ibtnNganhNghe1_Click(object sender, ImageClickEventArgs e)
        {
            if (lblNganhNghe2.Visible)
            {
                lblNganhNghe1.Text = lblNganhNghe2.Text;
                ibtnNganhNghe2_Click(sender, e);
            }
            else
            {
                ibtnNganhNghe1.Visible = false;
                lblNganhNghe1.Visible = false;
                ddlNganhNghe.Enabled = true;
                if (!lblNganhNghe3.Visible && !lblNganhNghe2.Visible)
                {
                    lblNganhNghe.Visible = false;
                }
            }
           

        }
    }
}
